package junit.framework;

import java.util.ArrayList;

import cleanstarxchange.server.*;


public class UserTest extends TestCase {

	     private User user;
	     private Stock stk;
	     private OwnedStock ownedstk;
	     
	     /**
	      * Sets up the test fixture. 
	      * (Called before every test case method.) 
	      */ 
	     protected void setUp() { 
	 		 stk = new Stock( "Brian Kang", "BKA", new Long(9), 13.0, new ArrayList<Double>() );
	 		 user = new User(123123, "John", "Rockefeller", false, 72500.00);
	 		 ownedstk = new OwnedStock(new Long(9), new Long(123123),3, 87.52 );

	     } 


	     /**
	      * Tears down the test fixture. 
	      * (Called after every test case method.) 
	      */ 
	     protected void tearDown() { 
	          stk = null; 
	          user = null;
	     } 


	     public void testInitialSetting() { 
	          assertEquals("Checking first name", "John", user.getFirstName());
	          assertEquals("Checking last name", "Rockefeller", user.getLastName());
	          assertEquals("Checking balance", 72500.0, user.getBalance());
	          assertEquals("Checking uid", new Long (123123), user.getUserID());
	          
	     } 
	     public void testBuyStock(){
	    	 //user.buyStock(new Long(1), 3, stk.getCurrentPrice());
	       
	     }
	     public void testSellStock(){
	    	 user.sellStock(ownedstk, 0, 80.02);
	         assertEquals("Quantity should not change", 3, ownedstk.getQuantity());
	         user.sellStock(ownedstk, 1, 80.02);
	         assertEquals("Quantity should be reduced to 2", 2, ownedstk.getQuantity());
	         user.sellStock(ownedstk, 7, 80.02);
	         assertEquals("Quantity should not change; transaction aborted", 2, ownedstk.getQuantity());
	         user.sellStock(ownedstk, 2, 80.02);
	         assertEquals("Quantity should not change", 0, ownedstk.getQuantity());
	    	     	 
	     }
	     public void testAssetValue(){
	    	 System.out.println(user.getAssetValue());
	    	 assertEquals("Current balance all $72500 in cash", 72500.0, user.getAssetValue());
	    	 //adding stock to thi user
	    	 ownedstk = new OwnedStock(new Long(9), new Long(123123),3, 87.52 );
	    	 user.setAssetValue(user.getBalance() + ownedstk.getPurchasePrice()* ownedstk.getQuantity());
	    	 System.out.println(user.getAssetValue());
	    	 assertEquals("Current balance all $72762.56 in total", 72762.56, user.getAssetValue());

	    	 
	     }

	     public void testForException() { 
	          try { 
	        	  user.sellStock(null, 10, stk.getCurrentPrice());
	        	  fail("Should raise an IndexOutOfBoundsException");
	          } 
	          catch (NullPointerException success) {
	          } 
	     } 

	
}
